package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.constant.CacheConstant;
import cn.com.dcsgo.domain.Prod;
import cn.com.dcsgo.dto.IndexImgAddDTO;
import cn.com.dcsgo.dto.IndexImgQueryDTO;
import cn.com.dcsgo.dto.IndexImgUpdateDTO;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.exception.BusinessException;
import cn.com.dcsgo.feign.ProdFeignClient;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.utils.AuthUtil;
import cn.com.dcsgo.vo.IndexImgVO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dcsgo.domain.IndexImg;
import cn.com.dcsgo.service.IndexImgService;
import cn.com.dcsgo.mapper.IndexImgMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 针对表【index_img(主页轮播图)】的数据库操作Service实现
 *
 * @author Dcsgo
 * @since 2025-10-26 13:09:56
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "cn.com.dcsgo.service.impl.IndexImgServiceImpl")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg>
        implements IndexImgService {
    @Resource
    private ProdFeignClient prodFeignClient;

    @Override
    public Page<IndexImg> getPage(PageDTO pageDTO, IndexImgQueryDTO indexImgQueryDTO) {
        Long shopId = AuthUtil.getShopId();
        Integer status = indexImgQueryDTO.getStatus();
        Page<IndexImg> page = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
        return baseMapper.selectPage(page,
                new LambdaQueryWrapper<IndexImg>()
                        .eq(shopId == null || !shopId.equals(1L), IndexImg::getShopId, shopId)
                        .eq(status != null, IndexImg::getStatus, status)
                        .orderByDesc(IndexImg::getSeq)
        );
    }

    @Override
    @CacheEvict(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_INDEX_IMG)
    public boolean save(IndexImgAddDTO indexImgAddDTO) {
        Long shopId = AuthUtil.getShopId();
        if (shopId == null) {
            throw new BusinessException("请先关联店铺后再操作");
        }
        IndexImg saveData = new IndexImg();
        BeanUtil.copyProperties(indexImgAddDTO, saveData);
        if (saveData.getStatus() == null) {
            saveData.setStatus(1);
        }
        saveData.setShopId(shopId);
        int row = baseMapper.insert(saveData);
        log.debug("Dcsgo:IndexImgServiceImpl:save:row:{}", row);
        return row > 0;
    }

    @Override
    public IndexImgVO getIndexImgVOById(Long imgId) {
        IndexImg indexImg = baseMapper.selectById(imgId);
        if (indexImg == null) {
            throw new BusinessException("轮播图不存在");
        }
        IndexImgVO indexImgVO = new IndexImgVO();
        BeanUtil.copyProperties(indexImg, indexImgVO);
        Long prodId = indexImg.getProdId();
        if (prodId != null) {
            Result<Prod> result = prodFeignClient.getBasicProdInfo(prodId);
            if (!result.getCode().equals(200)) {
                //TODO：商品服务崩了，我轮播图信息查询接口直接不可用；这似乎不太合理，我应该不关心被调用服务的状态；
                // 因为我都写了兜底回调，即使商品服务崩了兜底回调会保证整个我这个服务的可用性。
                throw new BusinessException(result.getMsg());
            }
            if (result.getData() == null) {
                throw new BusinessException("轮播图关联的商品不存在");
            }
            indexImgVO.setPic(result.getData().getPic());
            indexImgVO.setProdName(result.getData().getProdName());
        }
        return indexImgVO;
    }

    @Override
    @Cacheable(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_INDEX_IMG)
    public List<IndexImg> getSupperIndexImg() {
        return list(
                new LambdaQueryWrapper<IndexImg>()
                        .eq(IndexImg::getShopId, 1)
                        .eq(IndexImg::getStatus, 1)
                        .orderByDesc(IndexImg::getSeq)
        );
    }

    @Override
    @CacheEvict(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_INDEX_IMG)
    public boolean updateById(IndexImgUpdateDTO indexImgUpdateDTO) {
        IndexImg updateData = new IndexImg();
        BeanUtil.copyProperties(indexImgUpdateDTO, updateData);
        return super.updateById(updateData);
    }

    @Override
    @CacheEvict(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_INDEX_IMG)
    public boolean removeBatchByIds(List<Long> imgIds) {
        return super.removeBatchByIds(imgIds);
    }
}




